package com.example.springboot_admin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_admin.pojo.Spus;
import com.example.springboot_admin.pojo.Tags;
import com.example.springboot_admin.service.ICategoriesService;
import com.example.springboot_admin.service.ISkusService;
import com.example.springboot_admin.service.ISpusService;
import com.example.springboot_admin.service.ITagsService;
import com.example.springboot_admin.vo.ChangeTags;
import com.example.springboot_admin.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author svblack
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/spus")
public class SpusController {
    @Autowired
    ISpusService spusService;
    @Autowired
    ISkusService skusService;
    @Autowired
    ITagsService tagsService;
    @Autowired
    ICategoriesService categoriesService;

    @RequestMapping("list")
    IPage<Spus> list(@RequestParam(defaultValue = "1") int page,
                     @RequestParam(defaultValue = "5")int limit){
        return spusService.page(new Page<>(page,limit));
    }

    @RequestMapping("del")
    Integer del(int categoryid){
        spusService.removeById(categoryid);
        skusService.deleteBySpuId(categoryid);
        return 0;
    }

    @RequestMapping("doAdd")
    Integer doAdd(@RequestBody Spus spus){
        return spusService.save(spus)?0:1;
    }

    @RequestMapping("doEdit")
    Integer doEdit(@RequestBody Spus spus){
        UpdateWrapper<Spus> wrapper = new UpdateWrapper<>();
        if(spus.getName()!=null&&!spus.getName().isEmpty()){
            wrapper.set("name", spus.getName());
        }
        if(spus.getDescription()!=null&&!spus.getDescription().isEmpty()){
            wrapper.set("description", spus.getDescription());
        }
        if(spus.getMin_price()!=null){
            wrapper.set("min_price", spus.getMin_price());
        }
        if(spus.getImage_url()!=null&&!spus.getImage_url().isEmpty()){
            wrapper.set("image_url", spus.getImage_url());
        }
        if(spus.getStatus()!=null){
            wrapper.set("status", spus.getStatus());
        }
        wrapper.eq("spu_id", spus.getSpu_id());
        return spusService.update(wrapper)?0:1;
    }

    //搜索
    @RequestMapping("search")
    List<Spus> search(String keyword){
        return spusService.search(keyword);
    }

//    获取标签
    @RequestMapping("getTagsBySpuId")
    List<Tags> getTagsBySpuId(int spuid){
        return tagsService.getTagsBySpuId(spuid);
    }

//    改变商品tags
    @RequestMapping("changeTags")
    ResponseMsg changeTags(@RequestBody ChangeTags tags){
        return spusService.changeTags(tags);
    }

//    Restful传参{id}
    @RequestMapping("{id}")
    Spus getSpusById(@PathVariable int id){
        int categoryid=spusService.getById(id).getCategory_id();
        String categoryName=categoriesService.getById(categoryid).getName();
        Spus spus=spusService.getById(id);
        spus.setCategory_name(categoryName);
        return spus;
    }
}

